package com.lxj.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;

/**
 * @author lxj
 * @date 2021/7/30 12:46
 * @Description:
 */
@Configuration
public class RequestRateLimitConfig {
    /**
     * ip限流
     * @return
     */
    @Bean("hostAddrKeyResolver")
    @Primary
    public KeyResolver hostAddrKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }

    /**
     * api接口限流
     *
     * @return
     */
    @Bean("apiKeyResolver")
    public KeyResolver apiKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getPath().value());

    }
}
